
<!--       syslogmgr -->
<template>
  <a-card
    :bordered="false"
    :body-style="{ 'padding-bottom': '0px' }"
    :style="{ margin: '6px' }"
  >
    <a-form
      ref="searchFormRef"
      name="advanced_search"
      :model="searchFormState"
      class="ant-advanced-search-form"
    >
      <a-row :gutter="24">
        <a-col :span="3">
          <a-form-item label="日志类型" name="LogType">
            <a-select
              v-model:value="searchFormState.LogType"
              style="width: 100%"
              placeholder="请选择系统日志类型"
              :options="selectOption"
              @change="tableRef.refresh(true)"
            ></a-select>
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-form-item label="日志内容" name="LogInfo">
            <a-input
              v-model:value="searchFormState.LogInfo"
              placeholder="请输入日志内容"
              @keyup.enter="tableRef.refresh(true)"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="日志时间" name="logTime">
            <a-range-picker
              v-model:value="searchFormState.logTime"
              show-time
              :format="dayFormat"
            />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-button class="mr10" type="primary" @click="tableRef.refresh(true)"
          v-show="hasRole(route.meta.RolePower)">查询</a-button
          >
          <a-button class="xn-mg08" @click="reset"  v-show="hasRole(route.meta.RolePower)">重置</a-button>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <a-card :bordered="false" :style="{ margin: '6px' }">
    <s-table
      ref="tableRef"
      :columns="columns"
      :data="loadData"
      :alert="options.alert.show"
      bordered
      :row-key="(record) => record.UpdateTime"
      :tool-config="toolConfig"
      :row-selection="options.rowSelection"
      :scroll="{ x: 'max-content' }"
    >
      <template #operator>
        <a-space>
          <a-button type="primary" @click="exportData"  v-show="hasRole(route.meta.RolePower)">
            <template #icon><ToTopOutlined /></template>
            导出
          </a-button>
          <!--  <xn-batch-button
              buttonName="批量删除"
              icon="DeleteOutlined"
              buttonDanger
              :selectedRowKeys="selectedRowKeys"
              @batchCallBack="deleteBatchModule"
            /> -->
        </a-space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'UpdateTime'">
          {{ transitionTime(record.UpdateTime) }}
        </template>
       
      </template>
    </s-table>
  </a-card>
  <Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>
  
  <script setup name="syslogmgr">
import dayjs from "dayjs";
import { cloneDeep } from "lodash-es";
import { message } from "ant-design-vue";
import {
  transitionTime,
  getCurrentDateTime,
  getPreviousMonthDateTime,
} from "@/utils/time.js";
import { onMounted, ref } from "vue";
import Form from "../deptmgr/form.vue";
import syslogmgrApi from "@/api/systemManagement/syslogmgrApi";
import { useRoute } from "vue-router";
const route = useRoute();
const dayFormat = "YYYY/MM/DD HH:mm:ss";
const searchFormState = ref({
  UserID: 0,
  LogInfo: "",
  LogType: "",
  StartTime: "",
  EndTime: "",
  logTime: [
    dayjs(getPreviousMonthDateTime(), dayFormat),
    dayjs(getCurrentDateTime(), dayFormat),
  ],
});

const formRef = ref();
const searchFormRef = ref();
const tableRef = ref();
const toolConfig = {
  refresh: true,
  height: true,
  columnSetting: false,
  striped: false,
};
const columns = [
  {
    title: "用户ID",
    dataIndex: "UserID",
    align: "center",
  },
  {
    title: "日志时间",
    dataIndex: "UpdateTime",
    align: "center",
  },
  {
    title: "日志类型",
    dataIndex: "LogType",
    align: "center",
  },
  {
    title: "日志信息",
    dataIndex: "LogInfo",
    align: "center",
  },
  {
    title: "操作人",
    dataIndex: "UserName",
    align: "center",
  },
  /*  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    width: "200px",
  }, */
];

// 列表选择配置
const options = {
  alert: {
    show: false,
    clear: () => {
      selectedRowKeys = ref([]);
    },
  },
};
let searchAndExportFormParam = ref({});
const loadData = (parameter) => {
  const searchFormParam = cloneDeep(searchFormState.value);

  if (JSON.stringify(searchFormParam.logTime) != "[]") {
    searchFormParam.StartTime = dayjs(searchFormParam.logTime[0]).format(
      dayFormat
    );
    searchFormParam.EndTime = dayjs(searchFormParam.logTime[1]).format(
      dayFormat
    );
    delete searchFormParam.logTime;
  }
  // 把查询条件保存到导出条件中
  searchAndExportFormParam.value = searchFormParam;
  return syslogmgrApi
    .getSysLogs(Object.assign(searchFormParam, parameter))
    .then((res) => {
      return res.Data;
    });
};

let selectOption = ref();
const fetchSelectionOption = () => {
  syslogmgrApi.getSysLogTypes().then((res) => {
    let resData = res.Data.map((item) => {
      return {
        label: item,
        value: item,
      };
    });
    selectOption.value = resData;

    selectOption.value.unshift({
      label: "全部",
      value: "",
    });
    searchFormState.value.type = resData[0].label;
  });
};
onMounted(() => {
  fetchSelectionOption();
});
// 重置
const reset = () => {
  searchFormRef.value.resetFields();
  tableRef.value.refresh(true);
};

const exportData = () => {
  const hide = message.loading("正在导出数据，请稍后...", 0);

  syslogmgrApi.exportOutSysLogs(searchAndExportFormParam.value).then((res) => {
    if (!res.Result) {
      hide()
      message.error("导出失败" + res.ResultMsg);
      return
    }
    hide()
    message.success("导出成功,即将开始下载");
    let a = document.createElement("a");
    let downUrl = import.meta.env.VITE_API_BASEURL + "/ExportFile/" + res.Data;
    if (downUrl.includes("/ntmgrwebws.asmx")) {
      downUrl = downUrl.replace("/ntmgrwebws.asmx", "");
    }
    a.href = downUrl;
    a.download = res.Data;
    a.click();
    a = null;
  }).catch(() => {
      hide();
      message.error("导出失败");
    });
};
</script>
  